/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.merchant.DeliveryMember;
import com.neusoft.lohas.modules.ivm.service.merchant.DeliveryIndentService;
import com.neusoft.lohas.modules.ivm.service.merchant.DeliveryMemberService;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

/**
 * 人员Controller
 * @author Jackie.Liu
 * @version 2014-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/deliverymember")
public class DeliveryMemberController extends BaseController {

	@Autowired
	private DeliveryMemberService deliveryMemberService;
	
	@Autowired
	private DeliveryIndentService deliveryIndentService;
	
	@ModelAttribute
	public DeliveryMember get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return deliveryMemberService.get(id);
		}else{
			return new DeliveryMember();
		}
	}
	
	@RequiresPermissions("ivm:deliverymember:view")
	@RequestMapping(value = {"stats", ""})
	public String list(
			@RequestParam(value="startDate", required=false, defaultValue="") String startDate, 
			@RequestParam(value="endDate", required=false, defaultValue="") String endDate, 
			HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		Page<Map<String, Object>> page = new Page<Map<String,Object>>();
		page = deliveryIndentService.findDeliveryAccess(page, startDate, endDate, user.getId());
		int allIndents = 0; //总单数
		int allMembers = page.getList().size(); //总人数
		int avgIndents = 0;	//总日均单数（所有人日均单数之和）
		for(Map<String, Object> map:page.getList()){
			allIndents += Integer.valueOf(map.get("totalIndents").toString());
			avgIndents += Integer.valueOf(map.get("average").toString());
		}
		model.addAttribute("page", page);
		model.addAttribute("type", 1);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		model.addAttribute("allMembers", allMembers);
		model.addAttribute("allIndents", allIndents);
		if(allMembers == 0){
			model.addAttribute("avgIndents", 0);
		}else{
			model.addAttribute("avgIndents", avgIndents/allMembers);
		}
		return "modules/ivm/set/deliverymemberStats";
	}

	@RequiresPermissions("ivm:deliverymember:view")
	@RequestMapping(value = "list")
	public String manList(DeliveryMember deliverymember, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			deliverymember.setCreateBy(user);
		}
        Page<DeliveryMember> page = deliveryMemberService.find(new Page<DeliveryMember>(request, response), deliverymember); 
        model.addAttribute("page", page);
        model.addAttribute("type", deliverymember.getType());
		return "modules/ivm/set/deliverymemberList";
	}
	
	@RequiresPermissions("ivm:deliverymember:view")
	@RequestMapping(value = "form")
	public String form(DeliveryMember deliverymember, Model model) {
		if (StringUtils.isNotBlank(deliverymember.getName())) {
		}else {
			deliverymember.setMerchant(super.merchant);
		}
		model.addAttribute("deliverymember", deliverymember);
		model.addAttribute("type", deliverymember.getType());
		return "modules/ivm/set/deliverymemberForm";
	}

	@RequiresPermissions("ivm:deliverymember:edit")
	@RequestMapping(value = "save")
	public String save(DeliveryMember deliverymember, String oldName, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deliverymember)){
			return form(deliverymember, model);
		}
		if (!"true".equals(checkName(deliverymember.getMerchant().getId(),deliverymember.getType(),deliverymember.getName(),oldName))){
			addMessage(model, "保存人员'" + deliverymember.getName() + "'失败，姓名已存在");
			return form(deliverymember, model);
		}
		
		deliveryMemberService.save(deliverymember);
		addMessage(redirectAttributes, "保存人员'" + deliverymember.getName() + "'成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/deliverymember/list?type="+deliverymember.getType();
	}
	
	@RequiresPermissions("ivm:deliverymember:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, int type,  RedirectAttributes redirectAttributes) {
		deliveryMemberService.delete(id);
		addMessage(redirectAttributes, "删除人员成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/deliverymember/list?type="+type;
	}
	
	@ResponseBody
	@RequiresPermissions("ivm:deliverymember:edit")
	@RequestMapping(value = "checkName")
	public String checkName(String mId, int type, String name, String oldName) {
		if (name !=null && name.equals(oldName)) {
			return "true";
		} else if (name !=null && deliveryMemberService.findByName(type,name,mId) == null) {
			return "true";
		}
		return "false";
	}
	
}
